//
//  SettingArrowCell.swift
//  DoShare
//
//  Created by 申铭 on 2018/5/3.
//  Copyright © 2018年 eetrust. All rights reserved.
//

import UIKit

let kSettingArrowCell: String = "SettingArrowCell"
let kkSettingTitleLabelLeadingCons: CGFloat = 15

class SettingArrowCell: RoundCornerCell {
    
    @IBOutlet var titleLabel: UILabel!
    @IBOutlet var detailButton: UIButton!
    
    @IBOutlet var iconImageView: UIImageView!
    @IBOutlet weak var forwardImageView: UIImageView!
    
    @IBOutlet weak var forwardImageViewCons: NSLayoutConstraint!
    @IBOutlet var iconImageViewHeightCons: NSLayoutConstraint!
    @IBOutlet var iconImageViewWidthCons: NSLayoutConstraint!
    @IBOutlet var iconImageViewLeadingCons: NSLayoutConstraint!
    @IBOutlet var titleLabelLeadingCons: NSLayoutConstraint!
    @IBOutlet var detailBtnTrailingCons: NSLayoutConstraint!
    
    var model: SettingArrowCellModel? {
        didSet {
            if let attrTitle = model?.attrTitle {
                titleLabel.attributedText = attrTitle
            } else {
                titleLabel.text = model?.title
            }
            
            if let detailTitle = model?.detailTitle { // 有子标题
                detailButton.setTitle(detailTitle, for: .normal)
                detailButton.setImage(nil, for: .normal)
            }
            
            if let detailImageName = model?.detailImageName { // 有细节图片
                detailButton.setTitle("", for: .normal)
                detailButton.setImage(UIImage(named: detailImageName), for: .normal)
            }
            
            if let detailTitleColor = model?.detailTitleColor { // 子标题颜色
                detailButton.setTitleColor(detailTitleColor, for: .normal)
            }
            
            if let color = model?.color { // 颜色块
                detailButton.backgroundColor = color
            }
            
            if let iconImageName = model?.iconImageName { // 有图标
                let image = UIImage(named: iconImageName)
                iconImageViewWidthCons.constant = (image?.size.width)!
                iconImageViewHeightCons.constant = (image?.size.height)!
                iconImageView.image = image
            } else { // 没有则隐藏图标
                iconImageViewWidthCons.constant = 0
                iconImageViewHeightCons.constant = 0
                iconImageViewLeadingCons.constant = 0
                titleLabelLeadingCons.constant = kkSettingTitleLabelLeadingCons
            }
            
            // 显示小箭头
            if model?.isShowArrow ?? true {
                forwardImageView.isHidden = !(model?.isShowArrow)!
                forwardImageViewCons.constant = 7
                detailBtnTrailingCons.constant = 8
            } else {
                forwardImageView.isHidden = !(model?.isShowArrow)!
                forwardImageViewCons.constant = 0
                detailBtnTrailingCons.constant = 0
            }
            
            if model?.detailTitle != nil || model?.detailImageName != nil || model?.color != nil {
                detailButton.isHidden = false
            } else {
                detailButton.isHidden = true
            }
        }
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        
        titleLabel.font = kSettingCellTitleFont
        detailButton.isEnabled = false
        detailButton.titleLabel?.font = UIFont.systemFont(ofSize: 17)
    }

}
